package com.huikeportal.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huikeportal.dao.ChainMenuMapper;
import com.huikeportal.model.ChainMenuInfo;
import com.huikeportal.service.ChainMenuService;

@Service("chainMenuService")
public class ChainMenuServiceImpl implements ChainMenuService {

	@Autowired
	private ChainMenuMapper chainMenuMapper;

	public Map<String, Object> getChainMenuList(ChainMenuInfo chainMenuInfo)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ChainMenuInfo> menuList = chainMenuMapper
				.getChainMenuList(chainMenuInfo);
		// 查询身份列表用
		chainMenuInfo.setBelongString(null);
		List<ChainMenuInfo> allMenuList = chainMenuMapper
				.getChainMenuList(chainMenuInfo);
		//map.put("firstMenu", menuList);
		// 拼接右上角显示框
		Set<String> idList = new HashSet<String>();
		for (int i = 0; i < allMenuList.size(); i++) {
			idList.add(allMenuList.get(i).getMenuBelong());
		}
		map.put("idList", idList);
		// 拼接一级菜单
		Set<String> firstStringList = new HashSet<String>();
		for (int i = 0; i < menuList.size(); i++) {
			firstStringList.add(menuList.get(i).getParent1Id());
		}
		List<String> firstList = new ArrayList<String>(firstStringList);
		Collections.sort(firstList);
		List<ChainMenuInfo> oneList = new ArrayList<ChainMenuInfo>();
		for (int i = 0; i < firstList.size(); i++) {
			ChainMenuInfo oneChain = new ChainMenuInfo();
			oneChain.setMenuId(firstList.get(i));
			for (int j = 0; j < menuList.size(); j++) {
				if (firstList.get(i).equals(menuList.get(j).getParent1Id())) {
					oneChain.setMenuName(menuList.get(j).getParent1Name());
					break;
				}
			}
			oneList.add(oneChain);
		}
		map.put("oneList", oneList);
		// 拼接三级菜单
		// 如果没有指定一级菜单，则默认第一个菜单
		String selectedOne = firstList.get(0);
		if (chainMenuInfo.getParent1Id() != null
				&& !"".equals(chainMenuInfo.getParent1Id())) {
			selectedOne = chainMenuInfo.getParent1Id();
		}
		List<ChainMenuInfo> threeList = new ArrayList<ChainMenuInfo>();
		for (int i = 0; i < menuList.size(); i++) {
			if (selectedOne.equals(menuList.get(i).getParent1Id())) {
				threeList.add(menuList.get(i));
			}
		}
		map.put("threeList", threeList);
		return map;
	}

}
